package com.ruoyi.prod.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.prod.entity.po.ProdUseApply;
import com.ruoyi.prod.enums.AuditState;
import com.ruoyi.prod.enums.LogModuleEnum;
import com.ruoyi.prod.enums.LogTypeEnum;
import com.ruoyi.prod.enums.SysAction;
import com.ruoyi.prod.service.BaseLogService;
import com.ruoyi.prod.service.ProdUseApplyItemService;
import com.ruoyi.prod.service.ProdUseApplyService;
import com.ruoyi.prod.util.IsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 使用管理-资产使用 前端控制器
 * </p>
 *
 * @author 强子初
 * @since 2023-02-25
 */
@Api(tags = "使用管理-资产使用")
@RestController
@RequestMapping("/prod/use/apply")
public class ProdUseApplyController {

    @Autowired
    ProdUseApplyService baseService;

    @Autowired
    BaseLogService logService;

    /**
     * 详情
     */
    @ApiOperation("详情")
    @GetMapping("/{id}")
    public R<ProdUseApply> getInfo(@PathVariable String id) {
        return R.ok(baseService.get(id));
    }

    /**
     * 列表
     */
    @ApiOperation("列表")
    @PostMapping("/list")
    public R<List<ProdUseApply>> list(@Validated @RequestBody ProdUseApply condition) {
        return R.ok(baseService.list(condition));
    }

    /**
     * 分页
     */
    @ApiOperation("分页")
    @PostMapping("/page")
    public R<List<ProdUseApply>> page(@Validated @RequestBody ProdUseApply condition) {
        Page<ProdUseApply> page = PageHelper.startPage(condition.getPageNum(), condition.getPageSize());
        List<ProdUseApply> list = baseService.list(condition);
        return R.page(list, page.toPageInfo());
    }

    /**
     * 新增
     */
    @ApiOperation("新增")
    @PostMapping("/add")
    public R<?> add(@Validated @RequestBody ProdUseApply po) {

        baseService.add(po);

        // TODO 日志 需要改
        String content = String.format("新增了：%d个物品", po.getItem().size());
        logService.save(po.getId(), LogModuleEnum.USE_APPLY, LogTypeEnum.COMMIT, content);

        return R.ok(null, "新增成功");
    }

    /**
     * 删除
     */
    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public R<?> remove(@PathVariable String id) {
        AuditState state = baseService.getById(id).getAuditState();
        if (IsUtil.okAction(state, SysAction.DELETE)) throw new ServiceException("当前状态不允许修改");
        baseService.removeById(id);

        // TODO 日志 需要改
        logService.save(id, LogModuleEnum.PURCHASE, LogTypeEnum.DELETE);
        return R.ok(null, "删除成功");
    }

    /**
     * 修改
     */
    @ApiOperation("修改")
    @PutMapping("/edit")
    public R<?> edit(@Validated @RequestBody ProdUseApply po) {

        AuditState state = baseService.getById(po.getId()).getAuditState();
        SysAction action = po.getAction();
        if (IsUtil.okAction(state, action))
            throw new ServiceException("当前状态不允许修改");

        po.setAuditState(IsUtil.getAuditState(po.getAction()));

        baseService.saveOrUpdate(po);

        // 如果审核通过了，添加数据项 并且添加日志
        if (po.getAction() == SysAction.PASS) {
            baseService.auditPass(po);
        }

        // TODO 日志 需要改
        LogTypeEnum logType = IsUtil.getLogType(po.getAction());
        logService.save(po.getId(), LogModuleEnum.USE_APPLY, logType, po.getMessage());
        return R.ok(null, "修改成功");
    }
}